<?php

class Solutions_IndexController extends App_Controller_Action { 
/**
     * Отображает главную страницу
     */
    public function indexAction() 
    {
        $page = $this->_getParam('page');
    	$modelArticles = new Articles();
    	$articles = $modelArticles->getArticles();
        $paginator = new Zend_Paginator(new Zend_Paginator_Adapter_Array($articles));
        $paginator->setCurrentPageNumber($page);
        $this->view->solutions = $paginator;
    }

    /**
     * Страница из меню
     */    
    public function viewAction() 
    {
        $articleId = $this->_getParam('solutionId');
        $modelArticles = new Articles();
        $article = $modelArticles->getArticle($articleId);
        $this->view->solution = $article;
    }

    public function adminlistAction() {
    	if (!$this->isAuth()) {	$this->_helper->redirector->gotoRoute(array(), 'adminLogin'); }
    	$modelSolutions = new Articles();
    	$solutions = $modelSolutions->getArticles();
        $this->view->solutions = $solutions;
    }
    
    public function adminformAction() {
    	if (!$this->isAuth()) {	$this->_helper->redirector->gotoRoute(array(), 'adminLogin'); }
    	if ($this->_request->isPost()) {
    		$formSolution = new FormSolutions('');
    		if ($formSolution->isValid($this->_getAllParams())) {
    			$post = $this->_request->getPost();
    			$solution = new Articles();
    			$solution->setDataFromArray($post);
    			if ($solution->id) {
	    			$solution->update();
	    		} else {
    				$solution->insert();
	    		}
	    		$this->_helper->redirector->gotoRoute(array(), 'adminSolutions');
    		}
			$this->view->formSolutions = $formSolution;
    	} else {
	    	$id = $this->_getParam('solutionId');
	        $modelSolutions = new Articles();
			$solution = $modelSolutions->getArticle($id);
	        $this->view->solution = $solution;
			$formSolutions = new FormSolutions($solution);
			$this->view->formSolutions = $formSolutions;
    	}
    }

    public function admindeleteAction() {
    	if (!$this->isAuth()) {	$this->_helper->redirector->gotoRoute(array(), 'adminLogin'); }
    	$id = $this->_getParam('solutionId');
        $modelSolutions = new Articles();
        $res = $modelSolutions->delete('id='.$id);
		$this->_helper->redirector->gotoRoute(array(), 'adminSolutions');
    }
}
